/*package com.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReader {
	//private String FILE_PATH = "D:\\project workspace\\ClientConfigurationDetails.xml";
	private String FILE_PATH = "D:\\project workspace\\picSEEK\\test.xml";
	private int MAX_CONVARS = 20;
	private int MAX_SERVICES = 5;
	private NodeList firstLevelNodeList = null, secondlevelNodeList = null,
			thirdLevelNodeList = null, fourthLevelnodelist = null;
	private Node firstlevelNode = null, secondLevelNode = null,
			thirdlevelNode = null;
	private Element firstlevelElement = null, secondLevelElement = null,
			thirdLevelElement = null;
	private Document doc = null;

	public Document returnDocument() {

		try {
			File file = new File(FILE_PATH);
			//DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(file);
			doc.getDocumentElement().normalize();
		} catch (Exception e) {
			System.out.println("Error creating dom document");
		}
		return doc;
	}

	public XMLReader() {
		doc = returnDocument();
	}

	public void getfirstlevelElement(String apikey) {
		firstLevelNodeList = doc.getElementsByTagName("Apikey-" + apikey);
		firstLevelNodeList = doc.getElementsByTagName(apikey);
		firstlevelNode = firstLevelNodeList.item(0);
		firstlevelElement = (Element) firstlevelNode;

	}

	public String getDatabaseName(String apikey) {
		getfirstlevelElement(apikey);
		secondlevelNodeList = firstlevelElement
		.getElementsByTagName("Database");
		thirdLevelNodeList = ((Element) secondlevelNodeList.item(0))
				.getChildNodes();
		return ((Node) thirdLevelNodeList.item(0)).getNodeValue();
	}

	public ArrayList<String> getContextVariables(String apikey) {
		getfirstlevelElement(apikey);
		secondlevelNodeList = firstlevelElement
				.getElementsByTagName("ContextVariables");
		secondLevelNode = secondlevelNodeList.item(0);
		secondLevelElement = (Element) secondLevelNode;
		ArrayList<String> contextVariables = new ArrayList<String>();
		for (int i = 1; i <= MAX_CONVARS; i++) {
			String temp = "c" + i;
			thirdLevelNodeList = secondLevelElement.getElementsByTagName(temp);
			if (thirdLevelNodeList.getLength() == 0)
				break;
			fourthLevelnodelist = ((Element) thirdLevelNodeList.item(0))
					.getChildNodes();
			contextVariables.add(((Node) fourthLevelnodelist.item(0))
					.getNodeValue());
			System.out.println(((Node) fourthLevelnodelist.item(0))
					.getNodeValue());
		}
		
		
		return contextVariables;
	}

	public String getSearchEngine(String apikey) {
		getfirstlevelElement(apikey);
		secondlevelNodeList = firstlevelElement
				.getElementsByTagName("SearchEngines");
		secondLevelNode = secondlevelNodeList.item(0);
		secondLevelElement = (Element) secondLevelNode;
		thirdLevelNodeList = secondLevelElement.getChildNodes();
		return ((Node) thirdLevelNodeList.item(0)).getNodeValue();
	}

	public String getServiceAlgorithm(String apikey,String service) {
		getfirstlevelElement(apikey);
		secondlevelNodeList = firstlevelElement
				.getElementsByTagName("Services");
		secondLevelNode = secondlevelNodeList.item(0);
		secondLevelElement = (Element) secondLevelNode;
		HashMap<String, String> serviceAlgorithms = new HashMap<String, String>();
		for (int i = 1; i <= MAX_SERVICES; i++) {
			String temp = "S" + i;
			thirdLevelNodeList = secondLevelElement.getElementsByTagName(temp);
			if (thirdLevelNodeList.getLength() == 0)
				continue;
			fourthLevelnodelist = ((Element) thirdLevelNodeList.item(0))
					.getChildNodes();
			serviceAlgorithms.put(temp, ((Node) fourthLevelnodelist.item(0))
					.getNodeValue());

		}
		return serviceAlgorithms.get(service);
	}

	public static void main(String args[]) {
		XMLReader xmlreader = new XMLReader();
		System.out.println(xmlreader.getDatabaseName("1"));
		System.out.println(xmlreader.getSearchEngine("1"));
		ArrayList<String> temp = xmlreader.getContextVariables("1");
		for (int i = 0; i < temp.size(); i++) {
			System.out.println(temp.get(i));
		}
		ArrayList<String> temp1=xmlreader.getContextVariables("1");

	}
}
*/




package com.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReader {
	
	private String FILE_PATH = "C:\\Workspace\\picSEEK\\XMLRepository\\ClientConfigurationDetails.xml";
	private int MAX_CONVARS = 20;
	private int MAX_SERVICES = 5;
	private NodeList firstLevelNodeList = null, secondlevelNodeList = null,
			thirdLevelNodeList = null, fourthLevelnodelist = null;
	private Node firstlevelNode = null, secondLevelNode = null,
			thirdlevelNode = null;
	private Element firstlevelElement = null, secondLevelElement = null,
			thirdLevelElement = null;
	private Document doc = null;

	public Document returnDocument() throws Exception{
		FILE_PATH=ServerConfigurationReader.getClientConfigurationXMLPath();
		
			File file = new File(FILE_PATH);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(file);
			doc.getDocumentElement().normalize();
		
		return doc;
	}

	public XMLReader() throws Exception{
		doc = returnDocument();
	}

	public void getfirstlevelElement(String apikey) {
		//firstLevelNodeList = doc.getElementsByTagName("Apikey-" + apikey);
		firstLevelNodeList = doc.getElementsByTagName(apikey);
		firstlevelNode = firstLevelNodeList.item(0);
		firstlevelElement = (Element) firstlevelNode;

	}

	public String getDatabaseName(String apikey) {
		getfirstlevelElement(apikey);
		secondlevelNodeList = firstlevelElement.getElementsByTagName("DataBase");
		thirdLevelNodeList = ((Element) secondlevelNodeList.item(0))
				.getChildNodes();
		return ((Node) thirdLevelNodeList.item(0)).getNodeValue();
	}

	public ArrayList<String> getContextVariables(String apikey) {
		getfirstlevelElement(apikey);
		secondlevelNodeList = firstlevelElement
				.getElementsByTagName("ContextVariables");
		secondLevelNode = secondlevelNodeList.item(0);
		secondLevelElement = (Element) secondLevelNode;
		ArrayList<String> contextVariables = new ArrayList<String>();
		for (int i = 1; i <= MAX_CONVARS; i++) {
			String temp = "C" + i;
			thirdLevelNodeList = secondLevelElement.getElementsByTagName(temp);
			if (thirdLevelNodeList.getLength() == 0)
				break;
			fourthLevelnodelist = ((Element) thirdLevelNodeList.item(0))
					.getChildNodes();
			contextVariables.add(((Node) fourthLevelnodelist.item(0))
					.getNodeValue());
		}
		return contextVariables;
	}

	public String getSearchEngine(String apikey) {
		getfirstlevelElement(apikey);
		secondlevelNodeList = firstlevelElement
				.getElementsByTagName("SearchEngine");
		secondLevelNode = secondlevelNodeList.item(0);
		secondLevelElement = (Element) secondLevelNode;
		thirdLevelNodeList = secondLevelElement.getChildNodes();
		return ((Node) thirdLevelNodeList.item(0)).getNodeValue();
	}
	public  String getPassWord(String apikey){
		getfirstlevelElement(apikey);
		secondlevelNodeList = firstlevelElement
				.getElementsByTagName("Password");
		secondLevelNode = secondlevelNodeList.item(0);
		secondLevelElement = (Element) secondLevelNode;
		thirdLevelNodeList = secondLevelElement.getChildNodes();
		return ((Node) thirdLevelNodeList.item(0)).getNodeValue();
 
	}
	
	public String getServiceAlgorithm(String apikey) {
		getfirstlevelElement(apikey);
		secondlevelNodeList = firstlevelElement
				.getElementsByTagName("Service");
		secondLevelNode = secondlevelNodeList.item(0);
		secondLevelElement = (Element) secondLevelNode;
		thirdLevelNodeList = secondLevelElement.getChildNodes();
		/*HashMap<String, String> serviceAlgorithms = new HashMap<String, String>();
		for (int i = 1; i <= MAX_SERVICES; i++) {
			String temp = "S" + i;
			thirdLevelNodeList = secondLevelElement.getElementsByTagName(temp);
			if (thirdLevelNodeList.getLength() == 0)
				continue;
			fourthLevelnodelist = ((Element) thirdLevelNodeList.item(0))
					.getChildNodes();
			serviceAlgorithms.put(temp, ((Node) fourthLevelnodelist.item(0))
					.getNodeValue());

		}*/
		return ((Node) thirdLevelNodeList.item(0)).getNodeValue();
	}
	
	public boolean isValidapiKey(String apiKey){
		firstLevelNodeList = doc.getElementsByTagName(apiKey);
		if(firstLevelNodeList.item(0)==null){
			return false;
		}
		return true;
	}
	
	

}
